package ru.urfc.portal.developer_tasks.ejb_tests;

import java.util.Arrays;
import java.util.List;

import javax.ejb.EJB;

import org.hamcrest.CoreMatchers;

import ru.urfc.portal.dao.filter.Filter;
import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.model.Client;
import ru.urfc.portal.model.Branch;
import ru.urfc.portal.model.NotificationSettings;
import ru.urfc.portal.model.TmpContragent;
import ru.urfc.portal.model.accounting.Contragent;
import ru.urfc.portal.operations.ClientsSynchronizer;
import ru.urfc.portal.operations.ClientsSynchronizer.BranchContragents;

public class ClientsSynchronizerTestEjb extends BaseTestEjb {

	@EJB ClientsSynchronizer synchronizer;

	Branch branchNorth;
	Branch branchSouth;
	
	@Override
	protected void setUp() throws Exception {
		branchNorth = exactlyOne(find(Branch.class, "code", Branch.SVERD.getCode()));
		branchSouth = exactlyOne(find(Branch.class, "code", Branch.KURGAN.getCode()));
	}

	public void testCreateNewClients() {
		Contragent contragent = persist(new Contragent("qq"));
		Contragent contragent2 = persist(new Contragent("cc"));
		Filter filter = new Filter("contragentId", contragent.getId());
		filter.addEqual("branch.code", branchNorth.getCode());
		assertEquals(0, find(Client.class, filter).size());
		List<TmpContragent> tmpContragents = synchronizer.synchronize(
				new BranchContragents(branchNorth, Arrays.asList(contragent, contragent2)));
		assertEquals(1, find(Client.class, filter).size());
		assertEquals(2, tmpContragents.size());
		assertThat(Arrays.asList(
				tmpContragents.get(0).getBranch().getCode(), tmpContragents.get(1).getBranch().getCode()),
				CoreMatchers.is(Arrays.asList(branchNorth.getCode(), branchNorth.getCode())));
	}
	
	public void testUpdateContragentsSameIdsDifferentBranches() {
		Long id = 123456789L;
		Contragent contragent1 = new Contragent(id);
		Contragent contragent2 = new Contragent(id);
		persist(new Client(contragent1, branchNorth, new NotificationSettings()));
		persist(new Client(contragent2, branchSouth, new NotificationSettings()));
		Filter filter = new Filter("contragentId", id);
		assertEquals(2, find(Client.class, filter).size());
		List<TmpContragent> tmpContragents = synchronizer.synchronize(
				new BranchContragents(branchNorth, Arrays.asList(contragent1)),
				new BranchContragents(branchSouth, Arrays.asList(contragent2)));
		assertEquals(2, tmpContragents.size());
		TmpContragent tmpContragentNorth;
		TmpContragent tmpContragentSouth;
		if (tmpContragents.get(0).getBranch().getCode().equals(branchNorth.getCode())) {
			tmpContragentNorth = tmpContragents.get(0);
			tmpContragentSouth = tmpContragents.get(1); 
		} else {
			tmpContragentNorth = tmpContragents.get(1);
			tmpContragentSouth = tmpContragents.get(0); 
		}
		assertEquals(branchNorth.getCode(), tmpContragentNorth.getBranch().getCode());
		assertEquals(branchSouth.getCode(), tmpContragentSouth.getBranch().getCode());
		assertEquals(false, tmpContragentNorth.getClient().getId().equals(tmpContragentSouth.getClient().getId()));
	}
	
	public void testCreateTmpContragentWithNotNullClient() {
		Contragent contragent = persist(new Contragent("qq"));
		List<TmpContragent> result = synchronizer.synchronize(
				new BranchContragents(branchNorth, Arrays.asList(contragent)));
		assertEquals(1, result.size());
		assertNotNull(result.get(0).getClient());
	}
}
